library(dplyr)
library(readstata13)

## set working directory
## to replication folder 
survey <- read.dta13("Harris_Data/Harris 1972 Presidential Election Survey, study no. 2234/harris_s2234_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <-as.character(2234)

# study year (year)
survey$year <- 1972

# geographic data (urban)
summary(survey$S13)
survey$urban <- as.character(survey$S13)
str(survey$urban)

# geographic data (region)
summary(survey$S11)
survey$region <- as.character(survey$S11)

# respondent head of household (hh)
summary(survey$F2A)
survey$hh <- as.character(survey$F2A)

# increasing inequality (inequality)
summary(survey$P7_A)
survey$inequality <- as.character(survey$P7_A)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

## unions
survey$union.self <- as.character(survey$F7_1)
survey$union.self[survey$F7_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$F7_2)
survey$union.other[survey$F7_4 == "Yes"] <- "Not Sure"

# employment (employed)
summary(survey$F2B)
survey$employed <- as.character(survey$F2B) 

## employment.self
survey$employed.self <- NA

# occupation
summary(survey$F2C)
survey$occupation <- as.character(survey$F2C)

## occupation self
survey$occupation.self <- NA

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
summary(survey$F5)
survey$educ <- as.character(survey$F5)                       

# household income (income)
summary(survey$F6)
survey$income <- as.character(survey$F6)

# age
summary(survey$P1E)
survey$age <- as.character(survey$P1E)

# race
summary(survey$F8)
survey$race <- as.character(survey$F8)

# politics (party)
summary(survey$P1C)
survey$party <- as.character(survey$P1C)

# politics (ideology)
survey$ideology <- NA

# gender
summary(survey$F9)
survey$gender <- as.character(survey$F9)

# religion
summary(survey$F3A)
survey$religion <- as.character(survey$F3A)

# factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$P7_B)
survey$dontcount <- as.character(survey$P7_C)
survey$leftout <- as.character(survey$P7_E)

## question_place
survey$question_place <- "after party"

# subset
survey_2234 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                         "inequality", "inequality.variable", "union.self", "union.other",
                         "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                         "age", "race", "party", "ideology", "gender", "religion",
                         "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                         "question_place")]

# save file
#saveRDS(survey_2234, file = "Harris_Data/survey_2234.rds")
